//
//  MainTabBarController.m
//  梦香菜谱
//
//  Created by imac on 15/9/28.
//  Copyright (c) 2015年 imac. All rights reserved.
//

#import "ByTabBarController.h"

/** 子控制器 */
#import "ByInformationVC.h"         // 资讯
#import "ByJobVC.h"                 // 就业
#import "ByItemVC.h"                // 项目
#import "ByPersonalVC.h"            // 我的

@interface ByTabBarController ()

@end

@implementation ByTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    // 隐藏系统的TabBar
    self.tabBar.hidden = YES;
    
    // 创建定自定义的TabBar
    [self _createTabBar];
   
    // 创建子控制器
    [self _createViewControllers];
}

// 创建自定义的TabBar
- (void)_createTabBar {
    
    // 未选中状态图片
    NSArray *imageNames = @[
                            @"TabBar-资讯1",
                            @"TabBar-就业1",
                            @"TabBar-项目1",
                            @"TabBar-我的1",
                            ];
    // 选中状态图片
    NSArray *selectedImageNames = @[
                                    @"TabBar-资讯2",
                                    @"TabBar-就业2",
                                    @"TabBar-项目2",
                                    @"TabBar-我的2",
                                    ];
    // 标签栏标题
    NSArray *titles = @[
                        @"资讯",
                        @"就业",
                        @"项目",
                        @"我的",
                        ];
    
    // 创建整个TabBar
    _tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-49, SCREEN_WIDTH, 49)];
    self.tabBarView.backgroundColor = [UIColor whiteColor];
    _tabBarView.userInteractionEnabled = YES;
    _tabBarView.contentMode = UIViewContentModeScaleToFill;
    // 添加分隔线
    UIView *separateLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    separateLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [_tabBarView addSubview:separateLine];
    [self.view addSubview:_tabBarView];
    
    // 创建tabBar按钮
    CGFloat buttonWidth = SCREEN_WIDTH / titles.count;
    for (int i = 0; i < titles.count; i++) {
        // 未选中图片
        UIImage *buttonImage = [UIImage imageNamed:imageNames[i]];
        // 选中图片
        UIImage *selectedImage = [UIImage imageNamed:selectedImageNames[i]];
        ByTabBarButton *button = [[ByTabBarButton alloc] initWithFrame:CGRectMake(i * buttonWidth, 0, buttonWidth, 49) Image:buttonImage SelectedImage:selectedImage Title:titles[i]];
        button.tag = i + 10;
        if (i == 0) {
            // 刚开始设置第一个按钮为选中按钮
            button.isSelectedButton = YES;
            self.selectedButton = button;
        }
        [button addTarget:self action:@selector(_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBarView addSubview:button];
    }
}

// tabBar按钮点击事件
- (void)_buttonAction:(ByTabBarButton *)button {
    
    // 未登陆，且选中第2，3，则去登陆
    if (BY_USER == nil && (button.tag - 10 == 1 || button.tag - 10 == 2)) {
        ByLoginVC *vc = [[ByLoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        ByBaseNaviController *selectVC = self.selectedViewController;
        NSLog(@"%@", selectVC);
        [selectVC.visibleViewController presentViewController:navi animated:YES completion:nil];
        return;
    }
    
    // 切换视图控制器
    self.selectedIndex = button.tag -10;
    
    // 上一次选中的按钮状态改为no
    self.selectedButton.isSelectedButton = NO;
    // 当前被点击的按钮状态改为yes
    button.isSelectedButton = YES;
    // 记录当前选中按钮
    self.selectedButton = button;    
}


- (void)setSelectedIndex:(NSUInteger)selectedIndex{

    [super setSelectedIndex:selectedIndex];
    
    ByTabBarButton *button = [_tabBarView viewWithTag:(NSInteger)selectedIndex + 10];
    if ([button isKindOfClass:[UIButton class]]) {
        self.selectedButton.isSelectedButton = NO;
        button.isSelectedButton = YES;
        self.selectedButton = button;
    }
}


#pragma mark -- 创建控制器 --
- (void)_createViewControllers {
    /** 资讯 */
    ByInformationVC *vc1 = [[ByInformationVC alloc] init];
    ByBaseNaviController *navi1 = [[ByBaseNaviController alloc] initWithRootViewController:vc1];
    /** 就业 */
    ByJobVC *vc2 = [[ByJobVC alloc] init];
    ByBaseNaviController *navi2 = [[ByBaseNaviController alloc] initWithRootViewController:vc2];
    /** 项目 */
    ByItemVC *vc3 = [[ByItemVC alloc] init];
    ByBaseNaviController *navi3 = [[ByBaseNaviController alloc] initWithRootViewController:vc3];
    /** 我的 */
    ByPersonalVC *vc4 = [[ByPersonalVC alloc] init];
    ByBaseNaviController *navi4 = [[ByBaseNaviController alloc] initWithRootViewController:vc4];
    
    self.viewControllers = @[navi1,navi2,navi3,navi4];
}


#pragma mark - 标签栏隐藏和显示
// 隐藏标签栏
- (void)hideTabBar
{
    [UIView animateWithDuration:0.3 animations:^{
        _tabBarView.top = SCREEN_HEIGHT;
    }];
}


// 标签栏出现
- (void)showTabBar
{
    [UIView animateWithDuration:0.3 animations:^{
        _tabBarView.bottom = SCREEN_HEIGHT;
    }];
}
@end
